package com.meritdata.cloud.data.standard.directory.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.standard.directory.dao.IDataDomainDao;
import com.meritdata.cloud.data.standard.directory.dao.IDataLayerDao;
import com.meritdata.cloud.data.standard.directory.entity.TdDataDomain;
import com.meritdata.cloud.data.standard.directory.entity.TdDataLayer;
import com.meritdata.cloud.data.standard.directory.service.IDataLayerService;

@Service
public class DataLayerService implements IDataLayerService {
	
	@Autowired
	private IDataLayerDao dataLayerDao;
	
	@Autowired
	private IDataDomainDao dataDomainDao;

	@Override
	public ResultData create(String name, String code, String description, String userId) {
		ResultData resultData = new ResultData();
		
		List<TdDataLayer> layers = dataLayerDao.findEqualField("code", code);
		if (!CollectionUtils.isEmpty(layers)) {
			resultData.setStatus(false);
			resultData.setMsg("编码已存在");
			return resultData;
		}
		
		layers = dataLayerDao.findEqualField("name", name);
		if (!CollectionUtils.isEmpty(layers)) {
			resultData.setStatus(false);
			resultData.setMsg("名称已存在");
			return resultData;
		}
		
		TdDataLayer dataLayer = new TdDataLayer();
		dataLayer.setCode(code);
		dataLayer.setCreateTime(new Date());
		dataLayer.setCreator(userId);
		dataLayer.setDescription(description);
		dataLayer.setName(name);
		dataLayerDao.save(dataLayer);
		resultData.setData(dataLayer);
		return resultData;
	}

	@Override
	public ResultData delete(long id) {
		ResultData resultData = new ResultData();
		List<TdDataDomain> domains = dataDomainDao.findEqualField("layerId", id);
		if (!CollectionUtils.isEmpty(domains)) {
			resultData.setStatus(false);
			resultData.setMsg("数据层不为空，不允许删除");
			return resultData;
		}
		int delete = dataLayerDao.delete(id);
		resultData.setStatus(delete > 0);
		return resultData;
	}

	@Override
	public ResultData update(String name, String code, String description, long id, String userId) {
		ResultData resultData = new ResultData();
		
		TdDataLayer dataLayer = dataLayerDao.findOne(id);
		if (dataLayer == null) {
			resultData.setStatus(false);
			resultData.setMsg("数据层已不存在");
			return resultData;
		}
		
		List<TdDataLayer> layers = dataLayerDao.findEqualField("code", code);
		if (CollectionUtils.size(layers) > 1) {
			resultData.setStatus(false);
			resultData.setMsg("编码已存在");
			return resultData;
		}
		
		layers = dataLayerDao.findEqualField("name", name);
		if (CollectionUtils.size(layers) > 1) {
			resultData.setStatus(false);
			resultData.setMsg("名称已存在");
			return resultData;
		}
		
		dataLayer.setReviserId(userId);
		dataLayer.setUpdateTime(new Date());
		dataLayer.setCode(code);
		dataLayer.setName(name);
		dataLayer.setDescription(description);
		
		dataLayerDao.update(dataLayer);
		resultData.setData(dataLayer);
		return resultData;
	}

	@Override
	public List<TdDataLayer> list() {
		return dataLayerDao.findAll();
	}

	@Override
	public TdDataLayer get(long id) {
		return dataLayerDao.findOne(id);
	}

	@Override
	public List<TdDataLayer> tree() {
		List<TdDataLayer> layers = dataLayerDao.findAll();
		List<TdDataDomain> domains = dataDomainDao.findAll();
		
		Map<Long, TdDataLayer> layerMapping = new HashMap<>();
		for (TdDataLayer layer : layers) {
			layerMapping.put(layer.getId(), layer);
		}
		
		Map<Long, TdDataDomain> domainMapping = new HashMap<>();
		for (TdDataDomain domain : domains) {
			domainMapping.put(domain.getId(), domain);
		}
		
		for (TdDataDomain domain : domains) {
			Long layerId = domain.getLayerId();
			Long domainId = domain.getDomainId();
			
			TdDataDomain tdDataDomain = domainMapping.get(domainId);
			if (tdDataDomain != null) {
				List<Object> children = tdDataDomain.getChildren();
				if (children == null) {
					children = new ArrayList<>();
					tdDataDomain.setChildren(children);
				}
				children.add(domain);
				continue;
			}
			
			TdDataLayer tdDataLayer = layerMapping.get(layerId);
			if (tdDataLayer != null) {
				List<Object> children = tdDataLayer.getChildren();
				if (children == null) {
					children = new ArrayList<>();
					tdDataLayer.setChildren(children);
				}
				children.add(domain);
			}
		}
		
		return layers;
	}
	
}
